﻿using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using FDSimpleModelGenerator.Entities;
using FDSimpleModelGenerator.IServices;
using FDSimpleModelGenerator.Models.ClassTemplates;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace FDSimpleModelGenerator.Pages
{
    /// <summary>
    /// 类模板分类管理
    /// </summary>
    [Authorize]
    public class CategoryModel : PageModel
    {
        private readonly IWebHostEnvironment _hostingEnvironment;
        private readonly string TemplatePath;
        //private readonly IClassTemplateService _classTemplateService;
        private readonly IClassTemplateCategoryService _classTemplateCategoryService;
        public IList<ClassTemplateCategory> Categories { get; private set; }

        public CategoryModel(IWebHostEnvironment hostingEnvironment, IClassTemplateCategoryService classTemplateCategoryService)
        {
            _classTemplateCategoryService = classTemplateCategoryService;
            //_classTemplateService = classTemplateService;
            _hostingEnvironment = hostingEnvironment;

            TemplatePath = Path.Combine(_hostingEnvironment.WebRootPath, "Templates");
        }

        public void OnGet()
        {
            Categories = _classTemplateCategoryService.GetList();
        }

        public async Task<IActionResult> OnPostAdd([FromBody] ClassTemplateCategoryModel model)
        {
            if (!ModelChecker.Check(ModelState, out string errMsg))
            {
                return new JsonResult(new { Code = 400, Message = errMsg });
            }

            var res = await _classTemplateCategoryService.AddAsync(new ClassTemplateCategory
            {
                Name = model.Name,
                Description = model.Description
            });

            if (!res)
            {
                return new JsonResult(new { Code = 500, Message = "Error for add" });
            }

            return new JsonResult(new { Code = 0, Message = "Ok" });
        }

        public async Task<IActionResult> OnPostEdit([FromBody] ClassTemplateCategoryModel model)
        {
            if (!ModelChecker.Check(ModelState, out string errMsg))
            {
                return new JsonResult(new { Code = 400, Message = errMsg });
            }

            var category = await _classTemplateCategoryService.GetAsync(model.Id);
            if (category == null)
            {
                return new JsonResult(new { Code = 400, Message = "不存在" });
            }

            category.Name = model.Name;
            category.Description = model.Description;
            var res = await _classTemplateCategoryService.UpdateAsync(category);
            if (!res)
            {
                return new JsonResult(new { Code = 500, Message = "Error for update" });
            }
            return new JsonResult(new { Code = 0, Message = "Ok" });
        }
    }
}